import sys
import json
import logging
from importlib import import_module
from django.http import HttpResponse
from django.core.cache import cache
from .models import Port, PreProcess, Models, PostProcess
import dill as pickle


logging.basicConfig(filename='./log/service.log')
sys.path.append('./MODELS/')


def start_service():
    for port in Port.objects.all():
        port_name = 'port_' + port.name
        tmp = dict()
        tmp['pre'] = import_module(PreProcess.objects.get(pk=port.pre_process.pk).file)
        tmp['model'] = import_module(Models.objects.get(pk=port.model.pk).file)
        tmp['post'] = import_module(PostProcess.objects.get(pk=port.post_process.pk).file)
        cache.set(port_name, pickle.dumps(tmp))
    return HttpResponse('finished')


def predict(port_name, data):
    port_name = 'port_' + port_name
    port = pickle.loads(cache.get(port_name))
    logging.warning(str(data)[:20])
    tmp = data
    tmp = port['pre'].fun(tmp)
    tmp = port['model'].fun(tmp)
    tmp = port['post'].fun(tmp)
    result = HttpResponse(json.dumps(tmp, ensure_ascii=False))
    return result


def new_predict(data):
    port_name = 'port_' + data['Port']
    port = pickle.loads(cache.get(port_name))
    tmp = json.loads(data['Data'])
    tmp = port['pre'].fun(tmp)
    tmp = port['model'].fun(tmp)
    tmp = port['post'].fun(tmp)
    return tmp


def test():
    json_str = '{"\u524d\u6d77\u5e38\u8d37\u5ba2\u7684\u547d\u4e2d\u94f6\u884c\u673a\u6784\u6570": 0.0, "\u524d\u6d77\u5e38\u8d37\u5ba2\u7684\u547d\u4e2d\u6d88\u8d39\u91d1\u878d\u673a\u6784\u6570": 0.0, "\u8fd190\u5929\u5185\u501f\u6b3e\u603b\u7b14\u6570": 0.0, "\u5e38\u8d37\u5ba2\u673a\u6784\u67e5\u8be2\u603b\u6570": 5.0, "\u524d\u6d77\u5e38\u8d37\u5ba2\u7684\u547d\u4e2dP2P\u6216\u8005\u5c0f\u8d37\u673a\u6784\u6570": 3.0, "\u524d\u6d77\u5e38\u8d37\u5ba2\u7684\u8fd1\u4e09\u4e2a\u6708\u673a\u6784\u67e5\u8be2\u91cf": 1.0, "\u524d\u6d77\u5e38\u8d37\u5ba2\u7684\u8fd1\u516d\u4e2a\u6708\u673a\u6784\u67e5\u8be2\u91cf": 2.0, "\u8fd12\u5e74\u7533\u8bf7\u8d37\u6b3e\u6b21\u6570\uff08\u4fe1\u8054\u591a\u5934\u501f\u8d37 \uff09": 0.0, "\u8fd12\u5e74\u7533\u8bf7\u8d37\u6b3e\u653e\u6b3e\u6b21\u6570\uff08\u4fe1\u8054\u591a\u5934\u501f\u8d37 \uff09": 0.0, "\u8fd12\u5e74\u7533\u8bf7\u8d37\u6b3e\u903e\u671f\u5e73\u53f0\u6570\uff08\u4fe1\u8054\u591a\u5934\u501f\u8d37\uff09": 0.0, "\u8fd12\u5e74\u4fe1\u8d37\u5e73\u53f0\u6ce8\u518c\u4e2a\u6570\uff08\u4fe1\u8054\u591a\u5934\u501f\u8d37 \uff09": 0.0, "\u8fd12\u5e74\u7533\u8bf7\u8d37\u6b3e\u9a73\u56de\u6b21\u6570\uff08\u4fe1\u8054\u591a\u5934\u501f\u8d37 \uff09": 0.0, "\u8fd13\u4e2a\u6708\u53d1\u751f\u4ea4\u6613\u7684MCC\u79cd\u7c7b\u6570\uff08\u96c6\u5965\uff09": 1.0, "\u8fd112\u4e2a\u6708\u53d1\u751f\u4ea4\u6613\u7684MCC\u79cd\u7c7b\u6570\uff08\u96c6\u5965\uff09": 5.0, "\u8fc7\u53bb\u7b2c2\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u91d1\u989d\uff08\u96c6\u5965\uff09": 0.0, "\u8fc7\u53bb\u7b2c3\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u91d1\u989d\uff08\u96c6\u5965\uff09": 0.0, "\u8fc7\u53bb\u7b2c4\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u91d1\u989d\uff08\u96c6\u5965\uff09": 0.0, "\u8fd112\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u91d1\u989d\uff08\u96c6\u5965\uff09": 1500.0, "\u8fc7\u53bb\u7b2c2\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u7b14\u6570\uff08\u96c6\u5965\uff09": 0.0, "\u8fc7\u53bb\u7b2c3\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u7b14\u6570\uff08\u96c6\u5965\uff09": 0.0, "\u8fc7\u53bb\u7b2c4\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u7b14\u6570\uff08\u96c6\u5965\uff09": 0.0, "\u8fd112\u4e2a\u6708\u53d6\u73b0\u4ea4\u6613\u7b14\u6570\uff08\u96c6\u5965\uff09": 1.0, "\u829d\u9ebb\u5206_\u539f\u672c": 675.0, "\u597d\u4fe1\u5ea6\u5206_\u539f\u59cb": 674.0, "\u662f\u5426\u7855\u535a": 0.0, "\u662f\u5426211_985": 0.0, "\u6240\u6709\u4ea4\u6613\u7684\u603b\u91d1\u989d": 0.0, "\u6240\u6709\u4ea4\u6613\u7684\u603b\u7b14\u6570": 0.0, "\u65e0\u5361\u4ea4\u6613\u603b\u7b14\u6570": 0.0, "\u78c1\u6761\u5361\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "\u975e\u78c1\u6761\u5361\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "S0\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "\u975eS0\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "\u5237\u5361\u4ea4\u6613\u7ed3\u679c\u662f\u4f59\u989d\u4e0d\u8db3\u7684\u603b\u7b14\u6570": 0.0, "\u8fd1\u4e09\u4e2a\u6708\u6210\u529f\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "\u8fd1\u4e09\u4e2a\u6708\u6210\u529f\u4ea4\u6613\u603b\u7b14\u6570": 0.0, "\u8fd1\u4e09\u4e2a\u6708\u5931\u8d25\u4ea4\u6613\u603b\u91d1\u989d": 0.0, "\u8fd1\u4e09\u4e2a\u6708\u5931\u8d25\u4ea4\u6613\u603b\u7b14\u6570": 0.0, "\u6700\u8fd16\u4e2a\u6708\u4ea4\u6613\u91d1\u989d\u4e2d\u4f4d\u6570": 0.0, "\u6700\u8fd16\u4e2a\u6708\u4ea4\u6613\u91d1\u989d\u65b9\u5dee": 0.0, "T_1\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_2\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_3\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_4\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_5\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_6\u6708\u4ea4\u6613\u91d1\u989d": 0.0, "T_1\u6708\u4ea4\u6613\u7b14\u6570": 0.0, "T_2\u6708\u4ea4\u6613\u7b14\u6570": 0.0, "T_3\u6708\u4ea4\u6613\u7b14\u6570": 0.0, "\u6709\u4ea4\u6613\u5929\u6570": 0.0, "\u8fd13\u4e2a\u6708\u6709\u4ea4\u6613\u5929\u6570": 0.0, "\u6700\u540e\u4e00\u7b14\u4ea4\u6613\u4e0e\u5f53\u524d\u7684\u5929\u6570\u5dee": 0.0, "T_1\u6708\u4ea4\u6613\u7b14\u5747\u91d1\u989d": 0.0, "T_2\u6708\u4ea4\u6613\u7b14\u5747\u91d1\u989d": 0.0, "T_3\u6708\u4ea4\u6613\u7b14\u5747\u91d1\u989d": 0.0}'
    data = {
        'Port': 'port_name',
        'Data': json_str,
    }
    return new_predict(data)
